package com.mmy.controller.hotel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mmy.controller.Common;
import com.mmy.controller.UtilConstants;
import com.mmy.domain.constzone.TConstZone;
import com.mmy.domain.hotel.THotelBase;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.impl.constzone.ConstZoneServiceImpl;
import com.mmy.service.impl.hotel.THotelBaseServiceImpl;

@Controller
public class THotelBaseController {
	@Autowired
	THotelBaseServiceImpl tHotelBaseServiceImpl;
	@Autowired
	ConstZoneServiceImpl constZoneServiceImpl;
	@Autowired
	Common common;

	/**
	 * 函数功能说明：跳转创建酒店页面
	 *
	 * @param 参数说明 tHotelBase
	 * @return 返回值说明 jsp页面
	 * 
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.07.28
	 */
	@RequestMapping(value = "/tocreatehotelbase")
	public String ToCreateHotelBase(Model mHotelBase) {
		/* 查询所有区域 国家或者省市区 */

		List<TConstZone> db_TConstZoneProvince = constZoneServiceImpl.findAllProvince();
		mHotelBase.addAttribute("newTConstZoneProvince", db_TConstZoneProvince);
		List<TConstZone> db_TConstZone = constZoneServiceImpl.findAllCity();
		mHotelBase.addAttribute("newTConstZone", db_TConstZone);
		List<TConstZone> db_TConstZoneDistrict = constZoneServiceImpl.findAllDistrict();
		mHotelBase.addAttribute("newTConstZoneDistrict", db_TConstZoneDistrict);
		return "webbackground/hotelmanagement/createhotelbase";

	}

	/**
	 * 函数功能说明：创建酒店
	 *
	 * @param 参数说明 tHotelBase
	 * @return 返回值说明 jsp页面
	 * 
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.07.28
	 */
	@RequestMapping(value = "/savecreatehotelbase")
	public String saveCreateHotelBase(THotelBase tHotelBase, HttpSession session) {
		// 获得最大酒店编号
		tHotelBase.setHotelID(common.createBaseCode(UtilConstants.TableName.T_HOTEL_BASE));

		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");

		tHotelBase.setCreateDate(new Date());

		tHotelBase.setCreator(tSysUser.getUserName());
		tHotelBaseServiceImpl.save(tHotelBase.getHotelID());
		tHotelBaseServiceImpl.save(tHotelBase);
		return "redirect:hotelindex";

	}

	/**
	 * 函数功能说明： 修改酒店信息
	 *
	 * @param 参数说明 tHotelBase
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.08.18
	 */
	/* 点击修改按钮进入修改页面 */
	@RequestMapping("/toupdatehotelbase")
	public String updateHotelBaseCheckedFind(HttpServletRequest request, Model mHotel) {
		String iHotelID = request.getParameter("iHotelID");
		List<THotelBase> db_THotelBase = tHotelBaseServiceImpl.findByiHotelID(Integer.parseInt(iHotelID));
		mHotel.addAttribute("newTHotelBase", db_THotelBase.get(0));
		/* 查询所有区域 国家或者省市区 */
		/* 查询所有区域 洲与国家 */

		List<TConstZone> db_TConstZoneProvince = constZoneServiceImpl.findAllProvince();
		mHotel.addAttribute("newTConstZoneProvince", db_TConstZoneProvince);
		List<TConstZone> db_TConstZone = constZoneServiceImpl.findAllCity();
		mHotel.addAttribute("newTConstZone", db_TConstZone);
		List<TConstZone> db_TConstZoneDistrict = constZoneServiceImpl.findAllDistrict();
		mHotel.addAttribute("newTConstZoneDistrict", db_TConstZoneDistrict);

		return "webbackground/hotelmanagement/updatehotelbase";
	}

	/* 提交修改的酒店信息 */
	@RequestMapping(value = "/saveupdatehotelbase")
	public String updateHotelBase(THotelBase tHotelBase, HttpSession session) throws Exception {
		int iHotelID = tHotelBase.getHotelID();
		/* 定义一个新的实体类对象 */
		THotelBase dtHotelBase = tHotelBaseServiceImpl.findByiHotelID(iHotelID).get(0);
		dtHotelBase.setHotelName(tHotelBase.getHotelName());
		dtHotelBase.setHotelCode(tHotelBase.getHotelCode());
		dtHotelBase.setZoneID(tHotelBase.getZoneID());
		dtHotelBase.setAddress(tHotelBase.getAddress());
		dtHotelBase.setContact(tHotelBase.getContact());
		dtHotelBase.setTel(tHotelBase.getTel());
		dtHotelBase.setRate(tHotelBase.getRate());
		dtHotelBase.setType(tHotelBase.getType());
		dtHotelBase.setYear(tHotelBase.getYear());
		dtHotelBase.setHeight(tHotelBase.getHeight());
		dtHotelBase.setRoom(tHotelBase.getRoom());
		dtHotelBase.setIntro(tHotelBase.getIntro());
		dtHotelBase.setNotice(tHotelBase.getNotice());
		dtHotelBase.setFacilities(tHotelBase.getFacilities());
		dtHotelBase.setImgPath(tHotelBase.getImgPath());
		dtHotelBase.setEvaluation(tHotelBase.getEvaluation());
		dtHotelBase.setMemo(tHotelBase.getMemo());
		dtHotelBase.setModifyDate(new Date());
		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		dtHotelBase.setReviser(tSysUser.getUserName());

		tHotelBaseServiceImpl.save(dtHotelBase);

		return "redirect:hotelindex";

	}

	/**
	 * 函数功能说明： 删除酒店信息
	 *
	 * @param 参数说明 tHotelBase
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.08.18
	 */
	@PostMapping("/deletehotelbasechecked")
	@ResponseBody
	public String deleteHotelBaseChecked(String checkList) {
		String[] striHotelIDs = checkList.split(",");
		List<Integer> iHotelIDs = new ArrayList<>();

		for (String striHotelID : striHotelIDs) {
			iHotelIDs.add(Integer.parseInt(striHotelID));
		}

		tHotelBaseServiceImpl.deleteChecked(iHotelIDs);

		return "redirect:hotelindex";

	}

}
